package com.platform.sys.modular.datasource.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.platform.core.enums.CommonStatusEnum;
import com.platform.core.exception.ServiceException;
import com.platform.core.factory.PageFactory;
import com.platform.core.pojo.page.PageResult;
import com.platform.core.util.PoiUtil;
import com.platform.sys.modular.datasource.entity.DataSource;
import com.platform.sys.modular.datasource.enums.DataSourceExceptionEnum;
import com.platform.sys.modular.datasource.mapper.DataSourceMapper;
import com.platform.sys.modular.datasource.param.DataSourceParam;
import com.platform.sys.modular.datasource.service.DataSourceService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 数据源切换service接口实现类
 *
 * @author gyp
 * @date 2022-10-31 14:45:54
 */
@Service
public class DataSourceServiceImpl extends ServiceImpl<DataSourceMapper, DataSource> implements DataSourceService {

    @Override
    public PageResult<DataSource> page(DataSourceParam dataSourceParam) {
        QueryWrapper<DataSource> queryWrapper = new QueryWrapper<>();
        if (ObjectUtil.isNotNull(dataSourceParam)) {

            // 根据数据库名称 查询
            if (ObjectUtil.isNotEmpty(dataSourceParam.getName())) {
                queryWrapper.lambda().eq(DataSource::getName, dataSourceParam.getName());
            }
            // 根据驱动类型(1、Mysql；2、Oracle） 查询
            if (ObjectUtil.isNotEmpty(dataSourceParam.getType())) {
                queryWrapper.lambda().eq(DataSource::getType, dataSourceParam.getType());
            }
            // 根据JDBC的URL 查询
            if (ObjectUtil.isNotEmpty(dataSourceParam.getUrl())) {
                queryWrapper.lambda().eq(DataSource::getUrl, dataSourceParam.getUrl());
            }
            // 根据数据库账号 查询
            if (ObjectUtil.isNotEmpty(dataSourceParam.getAccount())) {
                queryWrapper.lambda().eq(DataSource::getAccount, dataSourceParam.getAccount());
            }
            // 根据数据库密码 查询
            if (ObjectUtil.isNotEmpty(dataSourceParam.getPassword())) {
                queryWrapper.lambda().eq(DataSource::getPassword, dataSourceParam.getPassword());
            }
            // 根据数据状态（0、正常；1、停用；3、删除） 查询
            if (ObjectUtil.isNotEmpty(dataSourceParam.getStatus())) {
                queryWrapper.lambda().eq(DataSource::getStatus, dataSourceParam.getStatus());
            }
            // 根据备注信息 查询
            if (ObjectUtil.isNotEmpty(dataSourceParam.getRemark())) {
                queryWrapper.lambda().eq(DataSource::getRemark, dataSourceParam.getRemark());
            }
        }
        return new PageResult<>(this.page(PageFactory.defaultPage(), queryWrapper));
    }

    @Override
    public List<DataSource> list(DataSourceParam dataSourceParam) {
        return this.list();
    }

    @Override
    public void add(DataSourceParam dataSourceParam) {
        DataSource dataSource = new DataSource();
        BeanUtil.copyProperties(dataSourceParam, dataSource);
        this.save(dataSource);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void delete(List<DataSourceParam> dataSourceParamList) {
        dataSourceParamList.forEach(dataSourceParam -> {
            this.removeById(dataSourceParam.getId());
        });
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void edit(DataSourceParam dataSourceParam) {
        DataSource dataSource = this.queryDataSource(dataSourceParam);
        BeanUtil.copyProperties(dataSourceParam, dataSource);
        this.updateById(dataSource);
    }

    @Override
    public DataSource detail(DataSourceParam dataSourceParam) {
        return this.queryDataSource(dataSourceParam);
    }

    /**
     * 获取数据源切换
     *
     * @author gyp
     * @date 2022-10-31 14:45:54
     */
    private DataSource queryDataSource(DataSourceParam dataSourceParam) {
        DataSource dataSource = this.getById(dataSourceParam.getId());
        if (ObjectUtil.isNull(dataSource)) {
            throw new ServiceException(DataSourceExceptionEnum.NOT_EXIST);
        }
        return dataSource;
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void enable(DataSourceParam dataSourceParam) {

        // 1、将所有的数据设置为不可用的状态
        LambdaQueryWrapper<DataSource> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(DataSource::getStatus, CommonStatusEnum.ENABLE.getCode());
        this.list(queryWrapper).forEach(entity -> {
            entity.setStatus(CommonStatusEnum.DISABLE.getCode());
            this.updateById(entity);
        });

        // 2、启用当前操作数据
        DataSource dataSource = this.queryDataSource(dataSourceParam);
        dataSource.setStatus(CommonStatusEnum.ENABLE.getCode());
        this.updateById(dataSource);
    }

    @Override
    public void export(DataSourceParam dataSourceParam) {
        List<DataSource> list = this.list(dataSourceParam);
        PoiUtil.exportExcelWithStream("DataSource.xls", DataSource.class, list);
    }

}
